package com.itcast.demo_one.kotlin

// Lambda 表达式

fun main(args: Array<String>) {
    // demo01()

    demo02()
}

fun demo01() {
    val numbers = listOf(1, 2, 3, 4, 5)  // flutter中List  /  鸿蒙中 数组

    // 1. 过滤出>3的数字
    // numbers.filter { it > 3 }
    // var newList = numbers.filter({num -> num > 3})
    // var newList = numbers.filter({it -> it > 3})
    // var newList = numbers.filter({it > 3})
    var newList = numbers.filter { it > 3}

    // 映射 - 每个元素加倍
    // val doubled = numbers.map({item -> item * 2})  // [2, 4, 6, 8, 10]
    val doubled = numbers.map {it * 2}

    // 查找 - 找到第一个大于3的元素
    val firstLarge = numbers.first { it > 3 }  // 4

    val lastLarge = numbers.last { it > 3 }

    val allLarge = numbers.all { it > 3 }

    val anyLarge = numbers.any { it > 3 }

    println(newList)
}


fun demo02() {
    fun calc(a: Int, b: Int, op: (Int, Int) -> Int): Int {
        return op(a, b)
    }

    // val res1 = calc(10, 20, {a, b -> a + b})
    val res1 = calc(10, 20 ){a, b -> a + b}
    println(res1)

    val res2 = calc(10, 20){a, b -> a - b}
    println(res2)

    val res3 = calc(10, 20){a, b -> a * b}
    println(res3)
}